home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / irisgl_vs_opengl / tessellate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.8 KB  |  247 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* tessellate.c  - open a window, clear the background, and render 
  19.  *    a simple nonconvex polygon with and without tessellation.
  20.  *    The untessellated polygon is drawn (incorrectly) on the left.
  21.  *    The corresponding tessellated object is drawn on the right.
  22.  *
  23.  *  The tessellated object is stored in a display list.
  24.  *
  25.  *  Escape key            - exit program
  26.  */
  27. #include <GL/gl.h>
  28. #include <GL/glu.h>
  29. #include <GL/glut.h>
  30. #include <math.h>
  31. #include <stdio.h>    /* for printf */
  32.  
  33. /*  Function Prototypes  */
  34.  
  35. GLvoid  initgfx( GLvoid );
  36. GLvoid  drawScene( GLvoid );
  37. GLvoid  reshape( GLsizei, GLsizei );
  38. GLvoid  keyboard( GLubyte, GLint, GLint );
  39.  
  40. void tesserror( GLenum errno );
  41. void my_glBegin( GLenum mode);
  42. void my_glEnd( void);
  43. void my_glVertex2fv( const GLfloat *v);
  44.  
  45. void printHelp( char * );
  46.  
  47. /* Global Definitions */
  48.  
  49. #define KEY_ESC    27    /* ascii value for the escape key */
  50.  
  51. #define NPOLYVERTS (sizeof(polyVerts)/sizeof(polyVerts[0]))
  52. #define COLOR_OFFSET 2 /* color offset in array of info about vertex */
  53. #define VSIZE 5
  54. /*      X  Y  R  G  B */
  55. static GLfloat polyVerts[][VSIZE]={
  56.     {  -2,  2, 0, 0, 0 },
  57.     {  -2, -2, 0, 0, 1 },
  58.     {   2, -2, 0, 1, 0 },
  59.     {   2,  2, 0, 1, 1 },
  60.     {   0,  0, 1, 0, 0 }
  61. };
  62.  
  63. /* Global Variables */
  64.  
  65. static GLUtriangulatorObj *tObj = NULL;
  66. static GLuint tObjList;
  67.  
  68. GLvoid
  69. main( int argc, char *argv[] )
  70. {
  71.     GLsizei width, height;
  72.  
  73.     glutInit( &argc, argv );
  74.  
  75.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  76.     height = glutGet( GLUT_SCREEN_HEIGHT );
  77.     glutInitWindowPosition( width / 4, height / 4 );
  78.     glutInitWindowSize( width / 2, height / 2 );
  79.     glutInitDisplayMode( GLUT_RGBA );
  80.     glutCreateWindow( argv[0] );
  81.  
  82.     initgfx();
  83.  
  84.     glutKeyboardFunc( keyboard );
  85.     glutReshapeFunc( reshape );
  86.     glutDisplayFunc( drawScene ); 
  87.  
  88.     printHelp( argv[0] );
  89.  
  90.     glutMainLoop();
  91. }
  92.  
  93. void
  94. printHelp( char *progname )
  95. {
  96.     fprintf(stdout, "\n%s - draws a simple nonconvex polygon twice;\n"
  97.         "untessellated on the left, tessellated on the right\n\n"
  98.         "Escape Key        - exit the program\n\n",
  99.         progname);
  100. }
  101.  
  102. GLvoid
  103. initgfx( GLvoid )
  104. {
  105.     int i;
  106.     GLdouble vertex[3];
  107.  
  108.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  109.  
  110.     /* Create the tessellation object */
  111.  
  112.     tObj = gluNewTess();
  113.  
  114.     /* Set the tessellation callbacks */
  115.  
  116.     gluTessCallback(tObj, GLU_BEGIN, my_glBegin);
  117.     gluTessCallback(tObj, GLU_VERTEX, my_glVertex2fv);
  118.     gluTessCallback(tObj, GLU_END, my_glEnd);
  119.     gluTessCallback(tObj, GLU_ERROR, tesserror);
  120.  
  121.     tObjList = glGenLists(1);
  122.     glNewList( tObjList, GL_COMPILE );
  123.         gluBeginPolygon(tObj);
  124.             for ( i = 0; i < NPOLYVERTS; i++ ) {
  125.                 vertex[0] = polyVerts[i][0];
  126.                 vertex[1] = polyVerts[i][1];
  127.                 vertex[2] = 0;
  128.                 /* vertex[] is the vertex of the polygon 
  129.                  * being tessellated. polyVerts[i] is 
  130.                  * the vertex plus associated data
  131.                  * (color, in this case) that get passed 
  132.                  * (by the GLU_VERTEX callback) to the 
  133.                  * my_glVertex2fv func. */
  134.                 gluTessVertex(tObj, vertex, &polyVerts[i]);
  135.             }
  136.         gluEndPolygon(tObj);
  137.     glEndList();
  138. }
  139.  
  140. GLvoid
  141. reshape( GLsizei width, GLsizei height )
  142. {
  143.     GLdouble    aspect, left, right, bottom, top;
  144.  
  145.     glViewport( 0, 0, width, height );
  146.  
  147.     aspect = (GLdouble) width / (GLdouble) height;
  148.  
  149.     /* make sure the window goes from [-10.0, 10.0] in the
  150.      * smallest dimension */
  151.     if ( aspect < 1.0 ) {
  152.         left = -10.0;
  153.         right = 10.0;
  154.         bottom = -10.0 * ( 1.0 / aspect );
  155.         top = 10.0 * ( 1.0 / aspect );
  156.     } else {
  157.         left = -10.0 * aspect;
  158.         right = 10.0 * aspect;
  159.         bottom = -10.0;
  160.         top = 10.0;
  161.     }
  162.  
  163.     glMatrixMode( GL_PROJECTION );
  164.     glLoadIdentity();
  165.     glOrtho( left, right, bottom, top, -1.0, 1.0 );
  166.     glMatrixMode( GL_MODELVIEW );
  167. }
  168.  
  169. GLvoid 
  170. keyboard( GLubyte key, GLint x, GLint y )
  171. {
  172.     switch (key) {
  173.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  174.         exit(0);
  175.     }
  176. }
  177.  
  178. GLvoid
  179. drawScene( GLvoid )
  180. {
  181.     int i;
  182.  
  183.     glClear( GL_COLOR_BUFFER_BIT );
  184.  
  185.     /* Draw untessellated object (incorrectly) */
  186.     glPushMatrix();
  187.         glTranslatef( -5.0, 0.0, 0.0 );
  188.         glBegin( GL_POLYGON );
  189.             for (i=0; i < NPOLYVERTS; i++) {
  190.                 glColor3fv( polyVerts[i]+COLOR_OFFSET );
  191.                 glVertex2fv( polyVerts[i] );
  192.             }
  193.         glEnd();
  194.     glPopMatrix();
  195.  
  196.     /* Draw tessellated objects (correctly) */
  197.     glPushMatrix();
  198.         glTranslatef( 5.0, 0.0, 0.0 );
  199.         glCallList( tObjList );
  200.     glPopMatrix();
  201.  
  202.     glFlush();
  203.     checkError("drawScene");
  204. }
  205.  
  206. /* The next four routines are for tessellation callbacks. */
  207.  
  208. /* Called if there is an error during tessellation. */
  209. void 
  210. tesserror ( GLenum errno ) 
  211. {
  212.     printf( "tessellation error: %s\n", gluErrorString( errno ) );
  213.     fflush( stdout );
  214.     exit( -1 );
  215. }
  216.  
  217. /* Called when a new primitive is started. */
  218. void 
  219. my_glBegin(GLenum mode)
  220. {
  221.     printf ("my_glBegin: mode is %d\n", mode);
  222.     glBegin (mode);
  223. }
  224.  
  225. /* Called when a primitive is finished. */
  226. void 
  227. my_glEnd(void)
  228. {
  229.     printf ("my_glEnd:\n");
  230.     glEnd();
  231. }
  232.  
  233. /* Called when a vertex of a primitive is generated by the 
  234.  * tessellator.  The parameter v is a pointer to the data 
  235.  * that was passed to the GLU_VERTEX callback.  In this case,
  236.  * the first two elements are coordinates of the vertex 
  237.  * and the next three specify the vertex color. */
  238. void 
  239. my_glVertex2fv(const GLfloat *v)
  240. {
  241.     printf ("my_glVertex2fv: v[0] = %g, v[1] = %g ", v[0], v[1]);
  242.     printf ("R = %g, G = %g, B = %g\n", v[2], v[3], v[4]);
  243.  
  244.     glColor3fv(v+COLOR_OFFSET);
  245.     glVertex2fv (v);
  246. }
  247.